#include "BDMainWindow.h"
#include <QFileDialog>
#include "TestProgressControl.pb.h"
#include "mmcogtestmsg.h"
#include "UIReceiver.h"
#include "UISender.h"


BDMainWindow::BDMainWindow(int id, QWidget *parent)
	: QMainWindow(parent)
	, m_pPainter(NULL)
	, m_bFirstUpdate(true)
	, m_iId(id)
{

	ui.setupUi(this);

	m_pPainter = new BDPainterWidget(this);
	ui.layout->addWidget(m_pPainter);

	m_pModel = new QStandardItemModel(this);
	m_pModel->setColumnCount(2);
	m_pModel->setHeaderData(0, Qt::Horizontal, QString("Item"));
	m_pModel->setHeaderData(1, Qt::Horizontal, QString("Value"));
	ui.tableView->setModel(m_pModel);
	// ui signal/slot
	connect(ui.backButton, SIGNAL(clicked()), parent, SLOT(OnStop()));
	connect(ui.saveButton, SIGNAL(clicked()), this, SLOT(OnStartRecord()));
	connect(ui.startButton, SIGNAL(clicked()), this, SLOT(OnStartTest()));
	// receiver signal/slot
	connect(UIReceiver::GetInstance(), SIGNAL(QPainterRefreshParam(const QVector<BDShape*>&)), this, SLOT(OnRefreshPainter(const QVector<BDShape*>&)));
	connect(UIReceiver::GetInstance(), SIGNAL(QDetailRefreshParam(const QMap<QString, QString>&)), this, SLOT(OnRefreshDetail(const QMap<QString, QString>&)));
	// other signal/slot
	connect(this, SIGNAL(TableViewDataChanged(QModelIndex, QModelIndex)), m_pModel, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)));
}

BDMainWindow::~BDMainWindow()
{

}



void BDMainWindow::OnStartRecord()
{

}

void BDMainWindow::OnStartTest()
{
	UISender sender;
	QVector<int> ids;
	ids.push_back(m_iId);
	sender.SendStartTest(ids);
}

void BDMainWindow::OnRefreshPainter(const QVector<BDShape*>& shapes)
{
	m_pPainter->setShapes(shapes);
	m_pPainter->update();
}

void BDMainWindow::OnRefreshDetail(const QMap<QString, QString>& details)
{
	if (m_bFirstUpdate) {
		m_bFirstUpdate = false;
		QMap<QString, QString>::const_iterator it = details.begin();
		int index = 0;
		for (; it != details.end(); ++it, ++index) {
			m_pModel->setItem(index, 0, new QStandardItem(it.key()));
			m_pModel->setItem(index, 1, new QStandardItem());
		}
	}
	
	QMap<QString, QString>::const_iterator it = details.begin();
	int index = 0;
	for (; it != details.end(); ++it, ++index) {
		m_pModel->item(index, 1)->setText(it.value());
	}

	emit TableViewDataChanged(m_pModel->index(0, 1), m_pModel->index(details.size() - 1, 1));
}